Page({
    data: {
      activities: [
      ].sort((a, b) => new Date(b.date) - new Date(a.date)) // 按照活动日期从新到旧排序
    },
  
    onLoad() {
        // 页面加载时直接从本地存储获取活动数据
        const activities = wx.getStorageSync('activities') || [];
        
        // 更新活动列表
        this.setData({
          activities: activities.sort((a, b) => new Date(b.date) - new Date(a.date)) // 重新排序
        });
      },
  
    // 点赞按钮点击事件
    onLikeClick(e) {
      const id = e.currentTarget.dataset.id;
      const activities = this.data.activities;
      const activity = activities.find(item => item.id === id);
  
      if (activity) {
        // 切换点赞状态
        activity.liked = !activity.liked;
  
        // 更新点赞人数
        if (activity.liked) {
          activity.likesCount += 1;
        } else {
          activity.likesCount -= 1;
        }
  
        this.setData({
          activities: activities, // 更新活动列表数据
        });
  
        // 更新本地存储中的活动数据
        wx.setStorageSync('activities', activities);
      }
    },
    // 删除按钮点击事件
    onDeleteClick(e) {
        const id = e.currentTarget.dataset.id;
        
        // 弹出确认框
        wx.showModal({
            title: '确认删除',
            content: '确定要删除这则活动吗？',
            success: (res) => {
                if (res.confirm) {
                    // 获取当前的活动列表
                    const activities = this.data.activities;
    
                    // 删除对应的活动
                    const updatedActivities = activities.filter(item => item.id !== id);
                    
                    // 更新活动数据
                    this.setData({
                        activities: updatedActivities
                    });

                    // 更新本地存储中的活动数据
                    wx.setStorageSync('activities', updatedActivities);

                    // 提示删除成功
                    wx.showToast({
                        title: '活动已删除',
                        icon: 'success',
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        });
    },
  
    // 查看详情点击事件
    onViewDetails(e) {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/activity-detail/activity-detail?id=${id}`, // 跳转到活动详情页面
      });
    },
  
    // 发布按钮点击事件
    onPublishClick() {
      wx.navigateTo({
        url: '/pages/post1/post1',
      });
    },
  
    // 下拉刷新触发的事件
    onPullDownRefresh() {
        const Activities = this.data.activities; // 当前页面显示的活动数据
        // 更新活动列表，并重新排序
        this.setData({
          activities: Activities.sort((a, b) => new Date(b.date) - new Date(a.date)) // 按日期重新排序
        });
      wx.stopPullDownRefresh();
      wx.showToast({
        title: '活动已刷新',
        icon: 'success',
        duration: 2000,
      });
    }
  });
  